<template>
  <scroll
    @scroll="scroll"
    ref="scroll"
    :listen-scroll="listenScroll"
    :probeType="probeType"
    :data2="cities"
    class="s-wrapper"
  >
    <!-- 当前城市 热门城市 城市列表 -->
    <div class="list">
      <div class="area" ref="current">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{ this.city }}</div>
          </div>
        </div>
      </div>
      <div class="area" ref="hot">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div
            class="button-wrapper"
            v-for="item in hotCities"
            :key="item.id"
            @click="clickHandler(item.name)"
          >
            <div class="button">
              {{ item.name }}
            </div>
          </div>
        </div>
      </div>
      <div class="area" ref="cities" v-for="(item, key) in cities" :key="key">
        <div class="title border-topbottom">{{ key }}</div>
        <div class="item-list">
          <div
            class="item border-bottom"
            v-for="innerItem in item"
            :key="innerItem.id"
            @click="clickHandler(innerItem.name)"
          >
            {{ innerItem.name }}
          </div>
        </div>
      </div>
    </div>
    <!-- 固定标题 -->
    <div class="fixedTitle" ref="fixedTitle" v-show="fixedTitle">
      {{ fixedTitle }}
    </div>
    <!-- 字母表 -->
    <ul
      class="alphabet"
      @touchstart.stop.prevent="onTouchStart"
      @touchmove.stop.prevent="onShortcutTouchMove"
      @touchend.stop
      v-show="letters.length"
    >
      <li
        class="item"
        ref="alphabetItem"
        :class="{ current: index === currentIndex }"
        v-for="(item, index) in letters"
        :data-index="index"
        :key="index"
      >
        {{ item }}
      </li>
    </ul>
    <div v-show="!letters.length" class="loading">
      <loading />
    </div>
  </scroll>
</template>

<script scoped>
import Loading from 'common/loading/Loading'
import Scroll from 'components/common/scroll/Scroll'
import { mapState, mapMutations } from 'vuex'
// 设置间距
const TITLE_HEIGHT = 60
export default {
  name: 'CityList',
  data () {
    return {
      scrollY: 0,
      currentIndex: 0,
      diff: -1
      // fixedTitle: ''
    }
  },
  props: {
    hotCities: Array,
    cities: Object,
    letters: Array
  },
  components: {
    Scroll,
    Loading
  },
  created () {
    this.probeType = 3
    this.listenScroll = true
    this.touch = {}
    this.listHeight = []
  },
  computed: {
    ...mapState({
      city: 'currentCity'
    }),
    fixedTitle () {
      if (this.scrollY === 0) {
        return ''
      }
      if (-this.scrollY < this.listHeight[0]) {
        console.log(this.scrollY, this.listHeight[0])
        return ''
      }
      return this.letters[this.currentIndex]
        ? this.letters[this.currentIndex]
        : ''
    }
  },
  methods: {
    ...mapMutations(['changeCity']),
    clickHandler (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
    scroll (pos) {
      this.scrollY = pos.y
    },
    _calculateHeight () {
      this.listHeight = []
      const cityList = this.$refs.cities
      const current = this.$refs.current.offsetHeight
      const hot = this.$refs.hot.offsetHeight
      let height = current + hot
      this.listHeight.push(height)
      for (let i = 0; i < cityList.length; i++) {
        const item = cityList[i]
        height += item.offsetHeight
        this.listHeight.push(height)
      }
    },
    _scrollTo (index) {
      console.log(index)
      if (!index && index !== 0) {
        return ''
      }
      if (index < 0) {
        index = 0
      } else if (index > this.letters.length - 1) {
        index = this.letters.length - 1
      }
      this.scrollY = -this.listHeight[index]
      console.log(this.scrollY)
      this.$refs.scroll.scrollToElement(this.$refs.cities[index], 0)
    },
    onTouchStart (e) {
      const touchIndex = e.target.getAttribute('data-index')
      console.log(touchIndex)
      const firstTouch = e.touches[0]
      this.touch.y1 = firstTouch.pageY
      this.touch.touchIndex = touchIndex
      this._scrollTo(touchIndex)
    },
    onShortcutTouchMove (e) {
      const firstTouch = e.touches[0]
      this.touch.y2 = firstTouch.pageY
      const height = this.$refs.alphabetItem[0].offsetHeight
      const delta = parseInt((this.touch.y2 - this.touch.y1) / height) | 0
      const index = parseInt(this.touch.touchIndex) + delta
      this._scrollTo(index)
    }
  },
  // mounted: function () {
  //   setTimeout(() => {使用延时器获得dom this.$refs.cities ,否则获取不到；或者在updated函数里直接调用
  //     this._calculateHeight()
  //   }, 200)
  // },
  updated () {
    this._calculateHeight()
  },
  watch: {
    scrollY (newY) {
      const listHeight = this.listHeight
      // 当滚动到顶部，newY>0
      if (-newY < listHeight[0]) {
        this.currentIndex = 0
        return 0
      }
      // 在中间部分滚动
      for (let i = 0; i < listHeight.length - 1; i++) {
        const height1 = listHeight[i]
        const height2 = listHeight[i + 1]
        if (-newY >= height1 && -newY < height2) {
          this.currentIndex = i
          this.diff = height2 + newY
          return 0
        }
      }
      // 当滚动到底部，且-newY大于最后一个元素的上限
      this.currentIndex = listHeight.length - 2
    },
    diff (newVal) {
      const fixedTop =
        newVal > 0 && newVal < TITLE_HEIGHT ? newVal - TITLE_HEIGHT : 0
      this.$refs.fixedTitle.style.transform = `translate3d(0,${fixedTop}px,0)`
    }
  }
}
</script>

<style lang='less' scoped>
.border-topbottom {
  &:before {
    border-color: #ccc;
  }
  &:after {
    border-color: #ccc;
  }
}
.border-bottom {
  &:before {
    border-color: #ccc;
  }
}
.s-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  .alphabet {
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 0.4rem;
    padding: 0.4rem 0;
    border-radius: 0.2rem;
    text-align: center;
    background: rgba(0, 0, 0, 0.3);
    font-family: Helvetica;
    .item {
      padding: 0.06rem;
      // line-height: 1;
      color: rgba(255, 255, 255, 0.5);
      font-size: 0.24rem;
    }
    .current {
      color: #fff;
      border-radius: 0.1rem;
      background-color: @bgColor;
    }
  }
  .fixedTitle {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 0.54rem;
    line-height: 0.54rem;
    background-color: #eee;
    padding-left: 0.2rem;
    color: #666;
    font-size: 0.26rem;
  }
  .loading{
    position: absolute;
    width: 100%;
    top:50%;
    transform:translateY(-50%);
    z-index: 99;
  }
}
.list {
  .title {
    line-height: 0.54rem;
    background-color: #eee;
    padding-left: 0.2rem;
    color: #666;
    font-size: 0.26rem;
  }
  .button-list {
    overflow: hidden;
    padding: 0.1rem 0.1rem;
    .button-wrapper {
      float: left;
      width: 33.33%;
      overflow: hidden;
      .button {
        margin: 0.1rem;
        padding: 0.1rem 0.1rem;
        text-align: center;
        border: 0.02rem solid #ccc;
        border-radius: 0.06rem;
        .ellipsis();
      }
    }
  }
  .item-list {
    .item {
      line-height: 0.76rem;
      padding-left: 0.2rem;
    }
  }
}
</style>
